package org.example;


import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;


import java.io.*;
import java.util.concurrent.*;


/**
 * Hello world!   -Xms128m -Xmx512m
 *
 */
public class App extends Application {
    ThreadPoolExecutor threadPoolExecutor =new ThreadPoolExecutor(3,200,0,TimeUnit.SECONDS,new LinkedBlockingQueue<>(500), Executors.defaultThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy());
    DirectoryChooser file = new DirectoryChooser();
    public static void main(String[] args )
    {
        Application.launch(App.class,args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane pane  = new Pane();
        Scene scene = new Scene(pane);
        primaryStage.setTitle("图像质量压缩工具·天猫用");
        primaryStage.setWidth(400);
        primaryStage.setHeight(200);

        Button chinkAction = new Button();
        chinkAction.setPrefWidth(300);
        chinkAction.setPrefHeight(50);
        chinkAction.setText("仅支持<=3900宽高压缩图像");
        chinkAction.setLayoutX(40);
        chinkAction.setLayoutY(90);
        TextField textField = new TextField();
        textField.setPromptText("处理状态");
        textField.setText("");
        textField.setAlignment(Pos.CENTER);
        textField.setDisable(true);
        textField.setLayoutX(40);
        textField.setLayoutY(20);
        textField.setPrefWidth(300);
        textField.setPrefHeight(50);
        primaryStage.setResizable(false);
        pane.getChildren().addAll(textField,chinkAction);
        primaryStage.setScene(scene);

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                System.exit(0);
            }
        });

        chinkAction.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                File result = file.showDialog(primaryStage);
                textField.setText("");
                threadPoolExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (App.class){
                            chinkAction.setDisable(true);
                            String pathFile = null;
                            try {
                                pathFile =  result.getPath();
                            }catch (NullPointerException e){
                                chinkAction.setDisable(false);
                            }
                            String[] file =null;
                            try {
                                file = new File(pathFile).list();
                            }catch (NullPointerException e){
                                chinkAction.setDisable(false);
                                return;
                            }
                          for (int i=0;i<file.length;i++){
                              File isFile = new File(pathFile,file[i]);

                              if (isFile.isFile()) {
                                  if(isFile.getPath().indexOf(".jpg")>-1 || isFile.getPath().indexOf(".png")>-1 || isFile.getPath().indexOf(".JPG")>-1 || isFile.getPath().indexOf(".PNG")>-1){
                                      try {
                                          threadPoolExecutor.execute(new threadRunnableImages(isFile,textField,chinkAction));
                                      }catch (Exception e) {
                                          chinkAction.setDisable(false);
                                          e.printStackTrace();
                                      }
                                  }
                              }
                          }
                        }
                    }
                });
            }
        });
        primaryStage.show();
    }


}
